﻿using UnityEngine;
using System.Collections;

public class UnitNoteScroll : MonoBehaviour {

    bool bDrag;
    int iListCount;

    bool bSmallThanMinBarSize;   // 본 사이즈가 최소사이즈보다 작을때 true

    float fBarDefaultYPos;  // 최초 +되는 y위치
    int iNumOfMaxNotes;  //한번에 창에보이는 최대 쪽지수

    float fMaxBarSize; // 바 길이 최대
    float fMinBarSize;

    float fMinBarPos; //최소길이 바일때 기본 위치

	// Use this for initialization
	void Start () {
        bSmallThanMinBarSize = false;
        bDrag = false;

        fBarDefaultYPos = 5f;
        iNumOfMaxNotes = 5;

        fMaxBarSize = 0.8f;
        fMinBarSize = 0.05f;
        fMinBarPos = 37.5f;

        iListCount = GameObject.Find("SceneDataMgr").GetComponent<SceneDataMgr>().UnitList.Count;

        if (iListCount > iNumOfMaxNotes)
        {
            transform.localScale = new Vector3(transform.localScale.x, fMaxBarSize - (((iListCount / iNumOfMaxNotes) - 1f) / 2f));
            transform.localPosition = new Vector3(transform.localPosition.x, (iListCount - iNumOfMaxNotes) * fBarDefaultYPos);

            if (transform.localScale.y < fMinBarSize)
            {
                transform.localScale = new Vector3(transform.localScale.x, fMinBarSize);
                transform.localPosition = new Vector3(transform.localPosition.x, fMinBarPos);
                bSmallThanMinBarSize = true;
                GameObject.Find("UnitNoteParent(Clone)").GetComponent<UnitNoteParent>().bSmallThanMinBarSize = true;
            }
        }
	}

    // Update is called once per frame
    void Update()
    {
        if (bDrag)
        {
            if (Input.GetMouseButton(0))
            {// Drag!
                DragObject();
            }
        }
    }

    void OnPress(bool isDown)
    {
        if (isDown)
            bDrag = true;
        else
            bDrag = false;
    }

    void DragObject()
    {
        if (bSmallThanMinBarSize)
        {
            transform.position = new Vector3(transform.position.x, UICamera.mainCamera.ScreenToWorldPoint(Input.mousePosition).y);

            if (transform.localPosition.y > fMinBarPos)
                transform.localPosition = new Vector3(transform.localPosition.x, fMinBarPos);
            else if (transform.localPosition.y < -fMinBarPos)
                transform.localPosition = new Vector3(transform.localPosition.x, -fMinBarPos);
            else
                transform.position = new Vector3(transform.position.x, UICamera.mainCamera.ScreenToWorldPoint(Input.mousePosition).y);
        }
        else
        {
            transform.position = new Vector3(transform.position.x, UICamera.mainCamera.ScreenToWorldPoint(Input.mousePosition).y);

            if (transform.localPosition.y > (iListCount - iNumOfMaxNotes) * fBarDefaultYPos)
                transform.localPosition = new Vector3(transform.localPosition.x, (iListCount - iNumOfMaxNotes) * fBarDefaultYPos);
            else if (transform.localPosition.y < (iListCount - iNumOfMaxNotes) * fBarDefaultYPos * -1)
                transform.localPosition = new Vector3(transform.localPosition.x, (iListCount - iNumOfMaxNotes) * fBarDefaultYPos * -1);
            else
                transform.position = new Vector3(transform.position.x, UICamera.mainCamera.ScreenToWorldPoint(Input.mousePosition).y);
        }
    }
}
